home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / dev / gcc / ppc-mos-gcc.lha / info / gcc.info-12 (.txt) < prev    next >
GNU Info File  |  2002-06-18  |  43KB  |  767 lines

  1. This is Info file gcc.info, produced by Makeinfo version 1.68 from the
  2. input file ./gcc.texi.
  3. INFO-DIR-SECTION Programming
  4. START-INFO-DIR-ENTRY
  5. * gcc: (gcc).                  The GNU Compiler Collection.
  6. END-INFO-DIR-ENTRY
  7.    This file documents the use and the internals of the GNU compiler.
  8.    Published by the Free Software Foundation 59 Temple Place - Suite 330
  9. Boston, MA 02111-1307 USA
  10.    Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
  11. 1999, 2000 Free Software Foundation, Inc.
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.    Permission is granted to copy and distribute modified versions of
  16. this manual under the conditions for verbatim copying, provided also
  17. that the sections entitled "GNU General Public License" and "Funding
  18. for Free Software" are included exactly as in the original, and
  19. provided that the entire resulting derived work is distributed under
  20. the terms of a permission notice identical to this one.
  21.    Permission is granted to copy and distribute translations of this
  22. manual into another language, under the above conditions for modified
  23. versions, except that the sections entitled "GNU General Public
  24. License" and "Funding for Free Software", and this permission notice,
  25. may be included in translations approved by the Free Software Foundation
  26. instead of in the original English.
  27. File: gcc.info,  Node: Naming Results,  Next: Min and Max,  Up: C++ Extensions
  28. Named Return Values in C++
  29. ==========================
  30.    GNU C++ extends the function-definition syntax to allow you to
  31. specify a name for the result of a function outside the body of the
  32. definition, in C++ programs:
  33.      TYPE
  34.      FUNCTIONNAME (ARGS) return RESULTNAME;
  35.      {
  36.        ...
  37.        BODY
  38.        ...
  39.      }
  40.    You can use this feature to avoid an extra constructor call when a
  41. function result has a class type.  For example, consider a function
  42. `m', declared as `X v = m ();', whose result is of class `X':
  43.      X
  44.      m ()
  45.      {
  46.        X b;
  47.        b.a = 23;
  48.        return b;
  49.      }
  50.    Although `m' appears to have no arguments, in fact it has one
  51. implicit argument: the address of the return value.  At invocation, the
  52. address of enough space to hold `v' is sent in as the implicit argument.
  53. Then `b' is constructed and its `a' field is set to the value 23.
  54. Finally, a copy constructor (a constructor of the form `X(X&)') is
  55. applied to `b', with the (implicit) return value location as the
  56. target, so that `v' is now bound to the return value.
  57.    But this is wasteful.  The local `b' is declared just to hold
  58. something that will be copied right out.  While a compiler that
  59. combined an "elision" algorithm with interprocedural data flow analysis
  60. could conceivably eliminate all of this, it is much more practical to
  61. allow you to assist the compiler in generating efficient code by
  62. manipulating the return value explicitly, thus avoiding the local
  63. variable and copy constructor altogether.
  64.    Using the extended GNU C++ function-definition syntax, you can avoid
  65. the temporary allocation and copying by naming `r' as your return value
  66. at the outset, and assigning to its `a' field directly:
  67.      X
  68.      m () return r;
  69.      {
  70.        r.a = 23;
  71.      }
  72. The declaration of `r' is a standard, proper declaration, whose effects
  73. are executed *before* any of the body of `m'.
  74.    Functions of this type impose no additional restrictions; in
  75. particular, you can execute `return' statements, or return implicitly by
  76. reaching the end of the function body ("falling off the edge").  Cases
  77.      X
  78.      m () return r (23);
  79.      {
  80.        return;
  81.      }
  82. (or even `X m () return r (23); { }') are unambiguous, since the return
  83. value `r' has been initialized in either case.  The following code may
  84. be hard to read, but also works predictably:
  85.      X
  86.      m () return r;
  87.      {
  88.        X b;
  89.        return b;
  90.      }
  91.    The return value slot denoted by `r' is initialized at the outset,
  92. but the statement `return b;' overrides this value.  The compiler deals
  93. with this by destroying `r' (calling the destructor if there is one, or
  94. doing nothing if there is not), and then reinitializing `r' with `b'.
  95.    This extension is provided primarily to help people who use
  96. overloaded operators, where there is a great need to control not just
  97. the arguments, but the return values of functions.  For classes where
  98. the copy constructor incurs a heavy performance penalty (especially in
  99. the common case where there is a quick default constructor), this is a
  100. major savings.  The disadvantage of this extension is that you do not
  101. control when the default constructor for the return value is called: it
  102. is always called at the beginning.
  103. File: gcc.info,  Node: Min and Max,  Next: Destructors and Goto,  Prev: Naming Results,  Up: C++ Extensions
  104. Minimum and Maximum Operators in C++
  105. ====================================
  106.    It is very convenient to have operators which return the "minimum"
  107. or the "maximum" of two arguments.  In GNU C++ (but not in GNU C),
  108. `A <? B'
  109.      is the "minimum", returning the smaller of the numeric values A
  110.      and B;
  111. `A >? B'
  112.      is the "maximum", returning the larger of the numeric values A and
  113.      B.
  114.    These operations are not primitive in ordinary C++, since you can
  115. use a macro to return the minimum of two things in C++, as in the
  116. following example.
  117.      #define MIN(X,Y) ((X) < (Y) ? : (X) : (Y))
  118. You might then use `int min = MIN (i, j);' to set MIN to the minimum
  119. value of variables I and J.
  120.    However, side effects in `X' or `Y' may cause unintended behavior.
  121. For example, `MIN (i++, j++)' will fail, incrementing the smaller
  122. counter twice.  A GNU C extension allows you to write safe macros that
  123. avoid this kind of problem (*note Naming an Expression's Type: Naming
  124. Types.).  However, writing `MIN' and `MAX' as macros also forces you to
  125. use function-call notation for a fundamental arithmetic operation.
  126. Using GNU C++ extensions, you can write `int min = i <? j;' instead.
  127.    Since `<?' and `>?' are built into the compiler, they properly
  128. handle expressions with side-effects;  `int min = i++ <? j++;' works
  129. correctly.
  130. File: gcc.info,  Node: Destructors and Goto,  Next: C++ Interface,  Prev: Min and Max,  Up: C++ Extensions
  131. `goto' and Destructors in GNU C++
  132. =================================
  133.    In C++ programs, you can safely use the `goto' statement.  When you
  134. use it to exit a block which contains aggregates requiring destructors,
  135. the destructors will run before the `goto' transfers control.
  136.    The compiler still forbids using `goto' to *enter* a scope that
  137. requires constructors.
  138. File: gcc.info,  Node: C++ Interface,  Next: Template Instantiation,  Prev: Destructors and Goto,  Up: C++ Extensions
  139. Declarations and Definitions in One Header
  140. ==========================================
  141.    C++ object definitions can be quite complex.  In principle, your
  142. source code will need two kinds of things for each object that you use
  143. across more than one source file.  First, you need an "interface"
  144. specification, describing its structure with type declarations and
  145. function prototypes.  Second, you need the "implementation" itself.  It
  146. can be tedious to maintain a separate interface description in a header
  147. file, in parallel to the actual implementation.  It is also dangerous,
  148. since separate interface and implementation definitions may not remain
  149. parallel.
  150.    With GNU C++, you can use a single header file for both purposes.
  151.      *Warning:* The mechanism to specify this is in transition.  For the
  152.      nonce, you must use one of two `#pragma' commands; in a future
  153.      release of GNU C++, an alternative mechanism will make these
  154.      `#pragma' commands unnecessary.
  155.    The header file contains the full definitions, but is marked with
  156. `#pragma interface' in the source code.  This allows the compiler to
  157. use the header file only as an interface specification when ordinary
  158. source files incorporate it with `#include'.  In the single source file
  159. where the full implementation belongs, you can use either a naming
  160. convention or `#pragma implementation' to indicate this alternate use
  161. of the header file.
  162. `#pragma interface'
  163. `#pragma interface "SUBDIR/OBJECTS.h"'
  164.      Use this directive in *header files* that define object classes,
  165.      to save space in most of the object files that use those classes.
  166.      Normally, local copies of certain information (backup copies of
  167.      inline member functions, debugging information, and the internal
  168.      tables that implement virtual functions) must be kept in each
  169.      object file that includes class definitions.  You can use this
  170.      pragma to avoid such duplication.  When a header file containing
  171.      `#pragma interface' is included in a compilation, this auxiliary
  172.      information will not be generated (unless the main input source
  173.      file itself uses `#pragma implementation').  Instead, the object
  174.      files will contain references to be resolved at link time.
  175.      The second form of this directive is useful for the case where you
  176.      have multiple headers with the same name in different directories.
  177.      If you use this form, you must specify the same string to `#pragma
  178.      implementation'.
  179. `#pragma implementation'
  180. `#pragma implementation "OBJECTS.h"'
  181.      Use this pragma in a *main input file*, when you want full output
  182.      from included header files to be generated (and made globally
  183.      visible).  The included header file, in turn, should use `#pragma
  184.      interface'.  Backup copies of inline member functions, debugging
  185.      information, and the internal tables used to implement virtual
  186.      functions are all generated in implementation files.
  187.      If you use `#pragma implementation' with no argument, it applies to
  188.      an include file with the same basename(1) as your source file.
  189.      For example, in `allclass.cc', giving just `#pragma implementation'
  190.      by itself is equivalent to `#pragma implementation "allclass.h"'.
  191.      In versions of GNU C++ prior to 2.6.0 `allclass.h' was treated as
  192.      an implementation file whenever you would include it from
  193.      `allclass.cc' even if you never specified `#pragma
  194.      implementation'.  This was deemed to be more trouble than it was
  195.      worth, however, and disabled.
  196.      If you use an explicit `#pragma implementation', it must appear in
  197.      your source file *before* you include the affected header files.
  198.      Use the string argument if you want a single implementation file to
  199.      include code from multiple header files.  (You must also use
  200.      `#include' to include the header file; `#pragma implementation'
  201.      only specifies how to use the file--it doesn't actually include
  202.      it.)
  203.      There is no way to split up the contents of a single header file
  204.      into multiple implementation files.
  205.    `#pragma implementation' and `#pragma interface' also have an effect
  206. on function inlining.
  207.    If you define a class in a header file marked with `#pragma
  208. interface', the effect on a function defined in that class is similar to
  209. an explicit `extern' declaration--the compiler emits no code at all to
  210. define an independent version of the function.  Its definition is used
  211. only for inlining with its callers.
  212.    Conversely, when you include the same header file in a main source
  213. file that declares it as `#pragma implementation', the compiler emits
  214. code for the function itself; this defines a version of the function
  215. that can be found via pointers (or by callers compiled without
  216. inlining).  If all calls to the function can be inlined, you can avoid
  217. emitting the function by compiling with `-fno-implement-inlines'.  If
  218. any calls were not inlined, you will get linker errors.
  219.    ---------- Footnotes ----------
  220.    (1) A file's "basename" was the name stripped of all leading path
  221. information and of trailing suffixes, such as `.h' or `.C' or `.cc'.
  222. File: gcc.info,  Node: Template Instantiation,  Next: Bound member functions,  Prev: C++ Interface,  Up: C++ Extensions
  223. Where's the Template?
  224. =====================
  225.    C++ templates are the first language feature to require more
  226. intelligence from the environment than one usually finds on a UNIX
  227. system.  Somehow the compiler and linker have to make sure that each
  228. template instance occurs exactly once in the executable if it is needed,
  229. and not at all otherwise.  There are two basic approaches to this
  230. problem, which I will refer to as the Borland model and the Cfront
  231. model.
  232. Borland model
  233.      Borland C++ solved the template instantiation problem by adding
  234.      the code equivalent of common blocks to their linker; the compiler
  235.      emits template instances in each translation unit that uses them,
  236.      and the linker collapses them together.  The advantage of this
  237.      model is that the linker only has to consider the object files
  238.      themselves; there is no external complexity to worry about.  This
  239.      disadvantage is that compilation time is increased because the
  240.      template code is being compiled repeatedly.  Code written for this
  241.      model tends to include definitions of all templates in the header
  242.      file, since they must be seen to be instantiated.
  243. Cfront model
  244.      The AT&T C++ translator, Cfront, solved the template instantiation
  245.      problem by creating the notion of a template repository, an
  246.      automatically maintained place where template instances are
  247.      stored.  A more modern version of the repository works as follows:
  248.      As individual object files are built, the compiler places any
  249.      template definitions and instantiations encountered in the
  250.      repository.  At link time, the link wrapper adds in the objects in
  251.      the repository and compiles any needed instances that were not
  252.      previously emitted.  The advantages of this model are more optimal
  253.      compilation speed and the ability to use the system linker; to
  254.      implement the Borland model a compiler vendor also needs to
  255.      replace the linker.  The disadvantages are vastly increased
  256.      complexity, and thus potential for error; for some code this can be
  257.      just as transparent, but in practice it can been very difficult to
  258.      build multiple programs in one directory and one program in
  259.      multiple directories.  Code written for this model tends to
  260.      separate definitions of non-inline member templates into a
  261.      separate file, which should be compiled separately.
  262.    When used with GNU ld version 2.8 or later on an ELF system such as
  263. Linux/GNU or Solaris 2, or on Microsoft Windows, g++ supports the
  264. Borland model.  On other systems, g++ implements neither automatic
  265. model.
  266.    A future version of g++ will support a hybrid model whereby the
  267. compiler will emit any instantiations for which the template definition
  268. is included in the compile, and store template definitions and
  269. instantiation context information into the object file for the rest.
  270. The link wrapper will extract that information as necessary and invoke
  271. the compiler to produce the remaining instantiations.  The linker will
  272. then combine duplicate instantiations.
  273.    In the mean time, you have the following options for dealing with
  274. template instantiations:
  275.   1. Compile your template-using code with `-frepo'.  The compiler will
  276.      generate files with the extension `.rpo' listing all of the
  277.      template instantiations used in the corresponding object files
  278.      which could be instantiated there; the link wrapper, `collect2',
  279.      will then update the `.rpo' files to tell the compiler where to
  280.      place those instantiations and rebuild any affected object files.
  281.      The link-time overhead is negligible after the first pass, as the
  282.      compiler will continue to place the instantiations in the same
  283.      files.
  284.      This is your best option for application code written for the
  285.      Borland model, as it will just work.  Code written for the Cfront
  286.      model will need to be modified so that the template definitions
  287.      are available at one or more points of instantiation; usually this
  288.      is as simple as adding `#include <tmethods.cc>' to the end of each
  289.      template header.
  290.      For library code, if you want the library to provide all of the
  291.      template instantiations it needs, just try to link all of its
  292.      object files together; the link will fail, but cause the
  293.      instantiations to be generated as a side effect.  Be warned,
  294.      however, that this may cause conflicts if multiple libraries try
  295.      to provide the same instantiations.  For greater control, use
  296.      explicit instantiation as described in the next option.
  297.   2. Compile your code with `-fno-implicit-templates' to disable the
  298.      implicit generation of template instances, and explicitly
  299.      instantiate all the ones you use.  This approach requires more
  300.      knowledge of exactly which instances you need than do the others,
  301.      but it's less mysterious and allows greater control.  You can
  302.      scatter the explicit instantiations throughout your program,
  303.      perhaps putting them in the translation units where the instances
  304.      are used or the translation units that define the templates
  305.      themselves; you can put all of the explicit instantiations you
  306.      need into one big file; or you can create small files like
  307.           #include "Foo.h"
  308.           #include "Foo.cc"
  309.           
  310.           template class Foo<int>;
  311.           template ostream& operator <<
  312.                           (ostream&, const Foo<int>&);
  313.      for each of the instances you need, and create a template
  314.      instantiation library from those.
  315.      If you are using Cfront-model code, you can probably get away with
  316.      not using `-fno-implicit-templates' when compiling files that don't
  317.      `#include' the member template definitions.
  318.      If you use one big file to do the instantiations, you may want to
  319.      compile it without `-fno-implicit-templates' so you get all of the
  320.      instances required by your explicit instantiations (but not by any
  321.      other files) without having to specify them as well.
  322.      g++ has extended the template instantiation syntax outlined in the
  323.      Working Paper to allow forward declaration of explicit
  324.      instantiations and instantiation of the compiler support data for
  325.      a template class (i.e. the vtable) without instantiating any of
  326.      its members:
  327.           extern template int max (int, int);
  328.           inline template class Foo<int>;
  329.   3. Do nothing.  Pretend g++ does implement automatic instantiation
  330.      management.  Code written for the Borland model will work fine, but
  331.      each translation unit will contain instances of each of the
  332.      templates it uses.  In a large program, this can lead to an
  333.      unacceptable amount of code duplication.
  334.   4. Add `#pragma interface' to all files containing template
  335.      definitions.  For each of these files, add `#pragma implementation
  336.      "FILENAME"' to the top of some `.C' file which `#include's it.
  337.      Then compile everything with `-fexternal-templates'.  The
  338.      templates will then only be expanded in the translation unit which
  339.      implements them (i.e. has a `#pragma implementation' line for the
  340.      file where they live); all other files will use external
  341.      references.  If you're lucky, everything should work properly.  If
  342.      you get undefined symbol errors, you need to make sure that each
  343.      template instance which is used in the program is used in the file
  344.      which implements that template.  If you don't have any use for a
  345.      particular instance in that file, you can just instantiate it
  346.      explicitly, using the syntax from the latest C++ working paper:
  347.           template class A<int>;
  348.           template ostream& operator << (ostream&, const A<int>&);
  349.      This strategy will work with code written for either model.  If
  350.      you are using code written for the Cfront model, the file
  351.      containing a class template and the file containing its member
  352.      templates should be implemented in the same translation unit.
  353.      A slight variation on this approach is to instead use the flag
  354.      `-falt-external-templates'; this flag causes template instances to
  355.      be emitted in the translation unit that implements the header
  356.      where they are first instantiated, rather than the one which
  357.      implements the file where the templates are defined.  This header
  358.      must be the same in all translation units, or things are likely to
  359.      break.
  360.      *Note Declarations and Definitions in One Header: C++ Interface,
  361.      for more discussion of these pragmas.
  362. File: gcc.info,  Node: Bound member functions,  Next: C++ Signatures,  Prev: Template Instantiation,  Up: C++ Extensions
  363. Extracting the function pointer from a bound pointer to member function
  364. =======================================================================
  365.    In C++, pointer to member functions (PMFs) are implemented using a
  366. wide pointer of sorts to handle all the possible call mechanisms; the
  367. PMF needs to store information about how to adjust the `this' pointer,
  368. and if the function pointed to is virtual, where to find the vtable, and
  369. where in the vtable to look for the member function.  If you are using
  370. PMFs in an inner loop, you should really reconsider that decision.  If
  371. that is not an option, you can extract the pointer to the function that
  372. would be called for a given object/PMF pair and call it directly inside
  373. the inner loop, to save a bit of time.
  374.    Note that you will still be paying the penalty for the call through a
  375. function pointer; on most modern architectures, such a call defeats the
  376. branch prediction features of the CPU.  This is also true of normal
  377. virtual function calls.
  378.    The syntax for this extension is
  379.      extern A a;
  380.      extern int (A::*fp)();
  381.      typedef int (*fptr)(A *);
  382.      
  383.      fptr p = (fptr)(a.*fp);
  384.    You must specify `-Wno-pmf-conversions' to use this extension.
  385. File: gcc.info,  Node: C++ Signatures,  Prev: Bound member functions,  Up: C++ Extensions
  386. Type Abstraction using Signatures
  387. =================================
  388.    In GNU C++, you can use the keyword `signature' to define a
  389. completely abstract class interface as a datatype.  You can connect this
  390. abstraction with actual classes using signature pointers.  If you want
  391. to use signatures, run the GNU compiler with the `-fhandle-signatures'
  392. command-line option.  (With this option, the compiler reserves a second
  393. keyword `sigof' as well, for a future extension.)
  394.    Roughly, signatures are type abstractions or interfaces of classes.
  395. Some other languages have similar facilities.  C++ signatures are
  396. related to ML's signatures, Haskell's type classes, definition modules
  397. in Modula-2, interface modules in Modula-3, abstract types in Emerald,
  398. type modules in Trellis/Owl, categories in Scratchpad II, and types in
  399. POOL-I.  For a more detailed discussion of signatures, see `Signatures:
  400. A Language Extension for Improving Type Abstraction and Subtype
  401. Polymorphism in C++' by Gerald Baumgartner and Vincent F. Russo (Tech
  402. report CSD-TR-95-051, Dept. of Computer Sciences, Purdue University,
  403. August 1995, a slightly improved version appeared in
  404. *Software--Practice & Experience*, 25(8), pp. 863-889, August 1995).
  405. You can get the tech report by anonymous FTP from `ftp.cs.purdue.edu'
  406. in `pub/gb/Signature-design.ps.gz'.
  407.    Syntactically, a signature declaration is a collection of member
  408. function declarations and nested type declarations.  For example, this
  409. signature declaration defines a new abstract type `S' with member
  410. functions `int foo ()' and `int bar (int)':
  411.      signature S
  412.      {
  413.        int foo ();
  414.        int bar (int);
  415.      };
  416.    Since signature types do not include implementation definitions, you
  417. cannot write an instance of a signature directly.  Instead, you can
  418. define a pointer to any class that contains the required interfaces as a
  419. "signature pointer".  Such a class "implements" the signature type.
  420.    To use a class as an implementation of `S', you must ensure that the
  421. class has public member functions `int foo ()' and `int bar (int)'.
  422. The class can have other member functions as well, public or not; as
  423. long as it offers what's declared in the signature, it is suitable as
  424. an implementation of that signature type.
  425.    For example, suppose that `C' is a class that meets the requirements
  426. of signature `S' (`C' "conforms to" `S').  Then
  427.      C obj;
  428.      S * p = &obj;
  429. defines a signature pointer `p' and initializes it to point to an
  430. object of type `C'.  The member function call `int i = p->foo ();'
  431. executes `obj.foo ()'.
  432.    Abstract virtual classes provide somewhat similar facilities in
  433. standard C++.  There are two main advantages to using signatures
  434. instead:
  435.   1. Subtyping becomes independent from inheritance.  A class or
  436.      signature type `T' is a subtype of a signature type `S'
  437.      independent of any inheritance hierarchy as long as all the member
  438.      functions declared in `S' are also found in `T'.  So you can
  439.      define a subtype hierarchy that is completely independent from any
  440.      inheritance (implementation) hierarchy, instead of being forced to
  441.      use types that mirror the class inheritance hierarchy.
  442.   2. Signatures allow you to work with existing class hierarchies as
  443.      implementations of a signature type.  If those class hierarchies
  444.      are only available in compiled form, you're out of luck with
  445.      abstract virtual classes, since an abstract virtual class cannot
  446.      be retrofitted on top of existing class hierarchies.  So you would
  447.      be required to write interface classes as subtypes of the abstract
  448.      virtual class.
  449.    There is one more detail about signatures.  A signature declaration
  450. can contain member function *definitions* as well as member function
  451. declarations.  A signature member function with a full definition is
  452. called a *default implementation*; classes need not contain that
  453. particular interface in order to conform.  For example, a class `C' can
  454. conform to the signature
  455.      signature T
  456.      {
  457.        int f (int);
  458.        int f0 () { return f (0); };
  459.      };
  460. whether or not `C' implements the member function `int f0 ()'.  If you
  461. define `C::f0', that definition takes precedence; otherwise, the
  462. default implementation `S::f0' applies.
  463. File: gcc.info,  Node: Gcov,  Next: Trouble,  Prev: C++ Extensions,  Up: Top
  464. `gcov': a Test Coverage Program
  465. *******************************
  466.    `gcov' is a tool you can use in conjunction with GNU CC to test code
  467. coverage in your programs.
  468.    This chapter describes version 1.5 of `gcov'.
  469. * Menu:
  470. * Gcov Intro::                     Introduction to gcov.
  471. * Invoking Gcov::           How to use gcov.
  472. * Gcov and Optimization::       Using gcov with GCC optimization.
  473. * Gcov Data Files::             The files used by gcov.
  474. File: gcc.info,  Node: Gcov Intro,  Next: Invoking Gcov,  Up: Gcov
  475. Introduction to `gcov'
  476. ======================
  477.    `gcov' is a test coverage program.  Use it in concert with GNU CC to
  478. analyze your programs to help create more efficient, faster running
  479. code.  You can use `gcov' as a profiling tool to help discover where
  480. your optimization efforts will best affect your code.  You can also use
  481. `gcov' along with the other profiling tool, `gprof', to assess which
  482. parts of your code use the greatest amount of computing time.
  483.    Profiling tools help you analyze your code's performance.  Using a
  484. profiler such as `gcov' or `gprof', you can find out some basic
  485. performance statistics, such as:
  486.    * how often each line of code executes
  487.    * what lines of code are actually executed
  488.    * how much computing time each section of code uses
  489.    Once you know these things about how your code works when compiled,
  490. you can look at each module to see which modules should be optimized.
  491. `gcov' helps you determine where to work on optimization.
  492.    Software developers also use coverage testing in concert with
  493. testsuites, to make sure software is actually good enough for a release.
  494. Testsuites can verify that a program works as expected; a coverage
  495. program tests to see how much of the program is exercised by the
  496. testsuite.  Developers can then determine what kinds of test cases need
  497. to be added to the testsuites to create both better testing and a better
  498. final product.
  499.    You should compile your code without optimization if you plan to use
  500. `gcov' because the optimization, by combining some lines of code into
  501. one function, may not give you as much information as you need to look
  502. for `hot spots' where the code is using a great deal of computer time.
  503. Likewise, because `gcov' accumulates statistics by line (at the lowest
  504. resolution), it works best with a programming style that places only
  505. one statement on each line.  If you use complicated macros that expand
  506. to loops or to other control structures, the statistics are less
  507. helpful--they only report on the line where the macro call appears.  If
  508. your complex macros behave like functions, you can replace them with
  509. inline functions to solve this problem.
  510.    `gcov' creates a logfile called `SOURCEFILE.gcov' which indicates
  511. how many times each line of a source file `SOURCEFILE.c' has executed.
  512. You can use these logfiles along with `gprof' to aid in fine-tuning the
  513. performance of your programs.  `gprof' gives timing information you can
  514. use along with the information you get from `gcov'.
  515.    `gcov' works only on code compiled with GNU CC.  It is not
  516. compatible with any other profiling or test coverage mechanism.
  517. File: gcc.info,  Node: Invoking Gcov,  Next: Gcov and Optimization,  Prev: Gcov Intro,  Up: Gcov
  518. Invoking gcov
  519. =============
  520.      gcov [-b] [-v] [-n] [-l] [-f] [-o directory] SOURCEFILE
  521.      Write branch frequencies to the output file, and write branch
  522.      summary info to the standard output.  This option allows you to
  523.      see how often each branch in your program was taken.
  524.      Display the `gcov' version number (on the standard error stream).
  525.      Do not create the `gcov' output file.
  526.      Create long file names for included source files.  For example, if
  527.      the header file `x.h' contains code, and was included in the file
  528.      `a.c', then running `gcov' on the file `a.c' will produce an
  529.      output file called `a.c.x.h.gcov' instead of `x.h.gcov'.  This can
  530.      be useful if `x.h' is included in multiple source files.
  531.      Output summaries for each function in addition to the file level
  532.      summary.
  533.      The directory where the object files live.  Gcov will search for
  534.      `.bb', `.bbg', and `.da' files in this directory.
  535.    When using `gcov', you must first compile your program with two
  536. special GNU CC options: `-fprofile-arcs -ftest-coverage'.  This tells
  537. the compiler to generate additional information needed by gcov
  538. (basically a flow graph of the program) and also includes additional
  539. code in the object files for generating the extra profiling information
  540. needed by gcov.  These additional files are placed in the directory
  541. where the source code is located.
  542.    Running the program will cause profile output to be generated.  For
  543. each source file compiled with -fprofile-arcs, an accompanying `.da'
  544. file will be placed in the source directory.
  545.    Running `gcov' with your program's source file names as arguments
  546. will now produce a listing of the code along with frequency of execution
  547. for each line.  For example, if your program is called `tmp.c', this is
  548. what you see when you use the basic `gcov' facility:
  549.      $ gcc -fprofile-arcs -ftest-coverage tmp.c
  550.      $ a.out
  551.      $ gcov tmp.c
  552.       87.50% of 8 source lines executed in file tmp.c
  553.      Creating tmp.c.gcov.
  554.    The file `tmp.c.gcov' contains output from `gcov'.  Here is a sample:
  555.                      main()
  556.                      {
  557.                 1      int i, total;
  558.      
  559.                 1      total = 0;
  560.      
  561.                11      for (i = 0; i < 10; i++)
  562.                10        total += i;
  563.      
  564.                 1      if (total != 45)
  565.            ######        printf ("Failure\n");
  566.                        else
  567.                 1        printf ("Success\n");
  568.                 1    }
  569.    When you use the `-b' option, your output looks like this:
  570.      $ gcov -b tmp.c
  571.       87.50% of 8 source lines executed in file tmp.c
  572.       80.00% of 5 branches executed in file tmp.c
  573.       80.00% of 5 branches taken at least once in file tmp.c
  574.       50.00% of 2 calls executed in file tmp.c
  575.      Creating tmp.c.gcov.
  576.    Here is a sample of a resulting `tmp.c.gcov' file:
  577.                      main()
  578.                      {
  579.                 1      int i, total;
  580.      
  581.                 1      total = 0;
  582.      
  583.                11      for (i = 0; i < 10; i++)
  584.      branch 0 taken = 91%
  585.      branch 1 taken = 100%
  586.      branch 2 taken = 100%
  587.                10        total += i;
  588.      
  589.                 1      if (total != 45)
  590.      branch 0 taken = 100%
  591.            ######        printf ("Failure\n");
  592.      call 0 never executed
  593.      branch 1 never executed
  594.                        else
  595.                 1        printf ("Success\n");
  596.      call 0 returns = 100%
  597.                 1    }
  598.    For each basic block, a line is printed after the last line of the
  599. basic block describing the branch or call that ends the basic block.
  600. There can be multiple branches and calls listed for a single source
  601. line if there are multiple basic blocks that end on that line.  In this
  602. case, the branches and calls are each given a number.  There is no
  603. simple way to map these branches and calls back to source constructs.
  604. In general, though, the lowest numbered branch or call will correspond
  605. to the leftmost construct on the source line.
  606.    For a branch, if it was executed at least once, then a percentage
  607. indicating the number of times the branch was taken divided by the
  608. number of times the branch was executed will be printed.  Otherwise, the
  609. message "never executed" is printed.
  610.    For a call, if it was executed at least once, then a percentage
  611. indicating the number of times the call returned divided by the number
  612. of times the call was executed will be printed.  This will usually be
  613. 100%, but may be less for functions call `exit' or `longjmp', and thus
  614. may not return everytime they are called.
  615.    The execution counts are cumulative.  If the example program were
  616. executed again without removing the `.da' file, the count for the
  617. number of times each line in the source was executed would be added to
  618. the results of the previous run(s).  This is potentially useful in
  619. several ways.  For example, it could be used to accumulate data over a
  620. number of program runs as part of a test verification suite, or to
  621. provide more accurate long-term information over a large number of
  622. program runs.
  623.    The data in the `.da' files is saved immediately before the program
  624. exits.  For each source file compiled with -fprofile-arcs, the profiling
  625. code first attempts to read in an existing `.da' file; if the file
  626. doesn't match the executable (differing number of basic block counts) it
  627. will ignore the contents of the file.  It then adds in the new execution
  628. counts and finally writes the data to the file.
  629. File: gcc.info,  Node: Gcov and Optimization,  Next: Gcov Data Files,  Prev: Invoking Gcov,  Up: Gcov
  630. Using `gcov' with GCC Optimization
  631. ==================================
  632.    If you plan to use `gcov' to help optimize your code, you must first
  633. compile your program with two special GNU CC options: `-fprofile-arcs
  634. -ftest-coverage'.  Aside from that, you can use any other GNU CC
  635. options; but if you want to prove that every single line in your
  636. program was executed, you should not compile with optimization at the
  637. same time.  On some machines the optimizer can eliminate some simple
  638. code lines by combining them with other lines.  For example, code like
  639. this:
  640.      if (a != b)
  641.        c = 1;
  642.      else
  643.        c = 0;
  644. can be compiled into one instruction on some machines.  In this case,
  645. there is no way for `gcov' to calculate separate execution counts for
  646. each line because there isn't separate code for each line.  Hence the
  647. `gcov' output looks like this if you compiled the program with
  648. optimization:
  649.            100  if (a != b)
  650.            100    c = 1;
  651.            100  else
  652.            100    c = 0;
  653.    The output shows that this block of code, combined by optimization,
  654. executed 100 times.  In one sense this result is correct, because there
  655. was only one instruction representing all four of these lines.  However,
  656. the output does not indicate how many times the result was 0 and how
  657. many times the result was 1.
  658. File: gcc.info,  Node: Gcov Data Files,  Prev: Gcov and Optimization,  Up: Gcov
  659. Brief description of `gcov' data files
  660. ======================================
  661.    `gcov' uses three files for doing profiling.  The names of these
  662. files are derived from the original *source* file by substituting the
  663. file suffix with either `.bb', `.bbg', or `.da'.  All of these files
  664. are placed in the same directory as the source file, and contain data
  665. stored in a platform-independent method.
  666.    The `.bb' and `.bbg' files are generated when the source file is
  667. compiled with the GNU CC `-ftest-coverage' option.  The `.bb' file
  668. contains a list of source files (including headers), functions within
  669. those files, and line numbers corresponding to each basic block in the
  670. source file.
  671.    The `.bb' file format consists of several lists of 4-byte integers
  672. which correspond to the line numbers of each basic block in the file.
  673. Each list is terminated by a line number of 0.  A line number of -1 is
  674. used to designate that the source file name (padded to a 4-byte
  675. boundary and followed by another -1) follows.  In addition, a line
  676. number of -2 is used to designate that the name of a function (also
  677. padded to a 4-byte boundary and followed by a -2) follows.
  678.    The `.bbg' file is used to reconstruct the program flow graph for
  679. the source file.  It contains a list of the program flow arcs (possible
  680. branches taken from one basic block to another) for each function which,
  681. in combination with the `.bb' file, enables gcov to reconstruct the
  682. program flow.
  683.    In the `.bbg' file, the format is:
  684.              number of basic blocks for function #0 (4-byte number)
  685.              total number of arcs for function #0 (4-byte number)
  686.              count of arcs in basic block #0 (4-byte number)
  687.              destination basic block of arc #0 (4-byte number)
  688.              flag bits (4-byte number)
  689.              destination basic block of arc #1 (4-byte number)
  690.              flag bits (4-byte number)
  691.              ...
  692.              destination basic block of arc #N (4-byte number)
  693.              flag bits (4-byte number)
  694.              count of arcs in basic block #1 (4-byte number)
  695.              destination basic block of arc #0 (4-byte number)
  696.              flag bits (4-byte number)
  697.              ...
  698.    A -1 (stored as a 4-byte number) is used to separate each function's
  699. list of basic blocks, and to verify that the file has been read
  700. correctly.
  701.    The `.da' file is generated when a program containing object files
  702. built with the GNU CC `-fprofile-arcs' option is executed.  A separate
  703. `.da' file is created for each source file compiled with this option,
  704. and the name of the `.da' file is stored as an absolute pathname in the
  705. resulting object file.  This path name is derived from the source file
  706. name by substituting a `.da' suffix.
  707.    The format of the `.da' file is fairly simple.  The first 8-byte
  708. number is the number of counts in the file, followed by the counts
  709. (stored as 8-byte numbers).  Each count corresponds to the number of
  710. times each arc in the program is executed.  The counts are cumulative;
  711. each time the program is executed, it attemps to combine the existing
  712. `.da' files with the new counts for this invocation of the program.  It
  713. ignores the contents of any `.da' files whose number of arcs doesn't
  714. correspond to the current program, and merely overwrites them instead.
  715.    All three of these files use the functions in `gcov-io.h' to store
  716. integers; the functions in this header provide a machine-independent
  717. mechanism for storing and retrieving data from a stream.
  718. File: gcc.info,  Node: Trouble,  Next: Bugs,  Prev: Gcov,  Up: Top
  719. Known Causes of Trouble with GCC
  720. ********************************
  721.    This section describes known problems that affect users of GCC.  Most
  722. of these are not GCC bugs per se--if they were, we would fix them.  But
  723. the result for a user may be like the result of a bug.
  724.    Some of these problems are due to bugs in other software, some are
  725. missing features that are too much work to add, and some are places
  726. where people's opinions differ as to what is best.
  727. * Menu:
  728. * Actual Bugs::              Bugs we will fix later.
  729. * Installation Problems::     Problems that manifest when you install GCC.
  730. * Cross-Compiler Problems::   Common problems of cross compiling with GCC.
  731. * Interoperation::      Problems using GCC with other compilers,
  732.                and with certain linkers, assemblers and debuggers.
  733. * External Bugs::    Problems compiling certain programs.
  734. * Incompatibilities::   GCC is incompatible with traditional C.
  735. * Fixed Headers::       GNU C uses corrected versions of system header files.
  736.                            This is necessary, but doesn't always work smoothly.
  737. * Standard Libraries::  GNU C uses the system C library, which might not be
  738.                            compliant with the ISO/ANSI C standard.
  739. * Disappointments::     Regrettable things we can't change, but not quite bugs.
  740. * C++ Misunderstandings::     Common misunderstandings with GNU C++.
  741. * Protoize Caveats::    Things to watch out for when using `protoize'.
  742. * Non-bugs::        Things we think are right, but some others disagree.
  743. * Warnings and Errors:: Which problems in your code get warnings,
  744.                          and which get errors.
  745. File: gcc.info,  Node: Actual Bugs,  Next: Installation Problems,  Up: Trouble
  746. Actual Bugs We Haven't Fixed Yet
  747. ================================
  748.    * The `fixincludes' script interacts badly with automounters; if the
  749.      directory of system header files is automounted, it tends to be
  750.      unmounted while `fixincludes' is running.  This would seem to be a
  751.      bug in the automounter.  We don't know any good way to work around
  752.      it.
  753.    * The `fixproto' script will sometimes add prototypes for the
  754.      `sigsetjmp' and `siglongjmp' functions that reference the
  755.      `jmp_buf' type before that type is defined.  To work around this,
  756.      edit the offending file and place the typedef in front of the
  757.      prototypes.
  758.    * There are several obscure case of mis-using struct, union, and
  759.      enum tags that are not detected as errors by the compiler.
  760.    * When `-pedantic-errors' is specified, GCC will incorrectly give an
  761.      error message when a function name is specified in an expression
  762.      involving the comma operator.
  763.    * Loop unrolling doesn't work properly for certain C++ programs.
  764.      This is a bug in the C++ front end.  It sometimes emits incorrect
  765.      debug info, and the loop unrolling code is unable to recover from
  766.      this error.
  767.